package com.longcai.piaojia.activity;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.longcai.piaojia.BaseApplication;
import com.longcai.piaojia.R;
import com.longcai.piaojia.conn.Conn;
import com.longcai.piaojia.util.ZXingUtils;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.scale.ScaleScreenHelperFactory;
import com.zcx.helper.util.UtilMD5;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

/**
 * Created by Administrator on 2016/11/21.
 */
public class MyCampaignActivity extends BaseActivity implements View.OnClickListener{

    @BoundView(R.id.title_back_rl) private RelativeLayout title_back_rl;
    @BoundView(R.id.title_name_tv) private TextView title_name_tv;
    @BoundView(R.id.mycanpaign_sv) private ScrollView mycanpaign_sv;
    @BoundView(R.id.mycanpaign_invite_code_tv) private TextView mycanpaign_invite_code_tv;
    @BoundView(R.id.mycanpaign_share_tv) private TextView mycanpaign_share_tv;//分享
    @BoundView(R.id.mycanpaign_qrcode_tv) private ImageView mycanpaign_qrcode_tv;//二维码

    private PopupWindow popupWindow;

    private  String phone, token;
    private String url;

    private Bitmap bitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mycanpaign);

        title_name_tv.setText("邀请有礼");

      //  mycanpaign_invite_code_tv.setText("【呼朋唤友，邀请有礼！】\n" + "邀请有礼，不花一分钱,不出一点力，朋友助力，即得实惠。\n注册即送5元儿童剧专享券，上不封顶。");
        mycanpaign_invite_code_tv.setText("【呼朋唤友，邀请有礼！】\n" + "邀请朋友一起免费约票");
        phone = BaseApplication.BasePreferences.readUserAccount();

        token = UtilMD5.MD5Encode(UtilMD5.MD5Encode(phone, "UTF-8") + UtilMD5.MD5Encode(Long.parseLong(phone) % 7 + "", "UTF-8"), "UTF-8");

        url = Conn.BASE_URL + Conn.WEB_REG_INDEX + "?phone=" + phone+"&token=" + token;

        bitmap = ZXingUtils.createQRImage(url, ScaleScreenHelperFactory.getInstance().getWidthHeight(330), ScaleScreenHelperFactory.getInstance().getWidthHeight(330));

//        // 二维码保存的地址路径
//        String qrImgPath = UtilSDCard.getSDCardPath() + "piaojia" + File.separator + "qrImg" + ".jpg";
//
//        Boolean isCreate = ZXingUtils.createQRImage(url, ScaleScreenHelperFactory.getInstance().getWidthHeight(330), ScaleScreenHelperFactory.getInstance().getWidthHeight(330), BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher), qrImgPath);
//
//        MyToast.show(context, isCreate + "", MyToast.LENGTH_SHORT);

        mycanpaign_qrcode_tv.setImageBitmap(bitmap);

        title_back_rl.setOnClickListener(this);
        mycanpaign_share_tv.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.title_back_rl:

                finish();

                break;


            case R.id.mycanpaign_share_tv:

                if (popupWindow == null) {
                    popupWindow = new PopupWindow(this);
                    popupWindow.setContentView(initView());
                    // PopupWindow必须设置高度和宽度
                    popupWindow.setBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.popwindow_bg));
                    popupWindow.setWidth(LinearLayout.LayoutParams.MATCH_PARENT); // 设置宽度
                    popupWindow.setHeight(LinearLayout.LayoutParams.MATCH_PARENT); // 设置popWin 高度
                    popupWindow.showAtLocation(mycanpaign_sv, Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0, 0);
                    popupWindow.setOutsideTouchable(true);// 设置后点击其他位置PopupWindow将消失
                } else if (popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                } else {
                    popupWindow.showAtLocation(mycanpaign_sv, Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0, 0);
                }

                break;

        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (bitmap!=null) {

            bitmap.recycle();

            bitmap = null;
        }

    }

    private View initView(){

        View view = View.inflate(this, R.layout.popwindow_my_setting_share, null);
        ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) view);
        RelativeLayout popwindow_main_rl = (RelativeLayout) view.findViewById(R.id.popwindow_main_rl);
        RelativeLayout my_setting_share_friend_rl = (RelativeLayout) view.findViewById(R.id.my_setting_share_friend_rl);
        RelativeLayout my_setting_share_pengyouquan_rl = (RelativeLayout) view.findViewById(R.id.my_setting_share_pengyouquan_rl);

        my_setting_share_friend_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                popupWindow.dismiss();
                showShare(MyCampaignActivity.this, 1);

            }
        });

        my_setting_share_pengyouquan_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                popupWindow.dismiss();
                showShare(MyCampaignActivity.this, 2);

            }
        });

        popwindow_main_rl.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                popupWindow.dismiss();

                return false;
            }
        });
        return view;

    }

    public void showShare(final Context context, int type) {

        Resources res = context.getResources();
        Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.share_icon);

        if(type==1){

            Wechat.ShareParams shareParams = new Wechat.ShareParams();
            shareParams.shareType = Platform.SHARE_WEBPAGE;

//            shareParams.setText("邀请有礼！不花一分钱，不出一点力，朋友助力，即得实惠。注册即送5元儿童剧专享券，上不封顶！");
//            shareParams.setText(share_time + "\n" + share_address);
            shareParams.setText("邀请朋友一起免费约票");
            shareParams.setTitle("【呼朋唤友，邀请有礼！】");

            shareParams.setImageData(bmp);

//            http://api.heiyoupiao.com/index.php/web/reg/index

            shareParams.setUrl(url);

            Platform wechat = ShareSDK.getPlatform(Wechat.NAME);
            wechat.setPlatformActionListener(new PlatformActionListener() {
                @Override
                public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {

//                    UtilToast.show(context, context.getString(R.string.goods_info_share_success_text));

                }

                @Override
                public void onError(Platform platform, int i, Throwable throwable) {

//                    UtilToast.show(context, context.getString(R.string.goods_info_share_failed_text));

                }

                @Override
                public void onCancel(Platform platform, int i) {

//                    UtilToast.show(context, context.getString(R.string.goods_info_share_failed_text));

                }
            });

            wechat.share(shareParams);

        }else{

            WechatMoments.ShareParams shareParams = new WechatMoments.ShareParams();
            shareParams.shareType = Platform.SHARE_WEBPAGE;
            shareParams.setText("邀请有礼！不花一分钱，不出一点力，朋友助力，即得实惠。注册即送5元儿童剧专享券，上不封顶！");
//            shareParams.setText(share_time + "\n" + share_address);

            shareParams.setTitle("【呼朋唤友，邀请有礼！】");

            shareParams.setImageData(bmp);

            shareParams.setUrl(url);

            Platform wechatmoents = ShareSDK.getPlatform (WechatMoments.NAME);
            wechatmoents.setPlatformActionListener(new PlatformActionListener() {
                @Override
                public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {

//                    UtilToast.show(context, context.getString(R.string.goods_info_share_success_text));

                }

                @Override
                public void onError(Platform platform, int i, Throwable throwable) {

//                    UtilToast.show(context, context.getString(R.string.goods_info_share_failed_text));

                }

                @Override
                public void onCancel(Platform platform, int i) {

//                    UtilToast.show(context, context.getString(R.string.goods_info_share_failed_text));

                }
            });

            wechatmoents.share(shareParams);

        }

    }


}

